package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.HotelTheme;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.HotelThemeMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IHotelThemeService;
import cn.wolfcode.luowowo.article.vo.DestInThemeVO;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class HotelThemeServiceImpl implements IHotelThemeService {
    @Autowired
    private HotelThemeMapper hotelThemeMapper;

    @Autowired
    private IDestinationService destinationService;

    @Override
    public List<HotelTheme> queryThemes() {
        List<HotelTheme> hotelThemes = hotelThemeMapper.selectAll();
        return new ArrayList<>(hotelThemes.subList(0,6));
    }

    @Override
    public List<DestInThemeVO> queryDestsByThemeId(Long themeId) {
        HotelTheme hotelTheme = hotelThemeMapper.selectByPrimaryKey(themeId);
        Long[] refIds = hotelTheme.getRefIds();
        List<Destination> destinations = destinationService.getDestsByIds(refIds);
        List<DestInThemeVO> list = new ArrayList<>();
        for (Destination destination : destinations) {
            DestInThemeVO vo = new DestInThemeVO();
            vo.setDestId(destination.getId());
            vo.setName(destination.getName());
            vo.setImg(destination.getCoverUrl());
            Destination dest = destinationService.getCountry(destination.getId());
            String country = dest == null ? destination.getName() : dest.getName();
            vo.setCountry(country);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<Destination> queryDestsByThemeContent(String content) {
        HotelTheme hotelTheme = hotelThemeMapper.selectByContent(content);
        return destinationService.getDestsByIds(hotelTheme.getRefIds());
    }
}
